<?php

defined( 'IN_DICE' ) or die( 'Access Error!' );

class ModelLogs extends base {
  
  function __construct() {
    parent::__construct();
  }
  
  /**
   * 将对象或者数组或者字符串写入到文件中去,一般为了调试
   *
   * @param mixd $obj
   * @param string $file
   * @return bool
   */
  public function debug( $obj, $file = 'debug.txt', $maxsize = 500 ) {
    $maxsize = max( 10, uint( $maxsize ) );
    $content = var_export( $obj, true ) . "\n";
    $file = DIR_DAT . $file;
    return file_put_contents( $file, $content, @filesize($file) < $maxsize * 1024 ? FILE_APPEND : null );
  }
  
  /**
   * 插入用户的钱币日志
   *
   * @param int $uid
   * @param int $clmode 给钱模式,参见 cfg.game.php
   * @param int $clchip 给的钱数
   * @param int $clflag 0加1减
   * @param int $clremark 标志:比如道具ID,订单ID等
   * @param string $cldesc 备注:标志写不了的就写在这里
   * @param bool $update 是否更新用户钱数
   * @return int
   */
  public function chiplog( $uid, $clmode, $clchip, $clflag = 0, $clremark = 0, $cldesc='', $update = false ) {
    if( ( !$uid = uint( $uid ) ) || ( !$clmode = uint( $clmode ) ) || ( !$clchip = uint( $clchip ) ) ) {
      return 0;
    }
    
    $mtime = time();
    $cldesc = model::db()->escape( $cldesc );
    $clflag = uint( $clflag );
    $clremark = uint( $clremark );
    $sql = "INSERT DELAYED INTO $this->chiplog SET uid='$uid',clmode='$clmode',
            clflag='$clflag',clchip='$clchip',clremark='$clremark',cldesc='$cldesc',
            mtime='$mtime'";
      model::db()->exe( $sql );
      $flag = model::db()->update_rows();
      model::logs()->logtouser( $uid, $clmode, $mtime, $clchip, $clremark, $cldesc ); //记录此事件
      
      if( $update === true ) { //加钱
        $flag = model::member()->setmoney( $uid, $clchip, $clflag );
      }
      return $flag;
  }
  
}

